from django.shortcuts import render

from django.http import JsonResponse
from django.db.models import Count
from web import models
import collections
import datetime
import time


def dashboard(request, project_id):
    status_dict = collections.OrderedDict()  # 创建一个有序字典
    for key, text in models.Issues.status_choices:
        status_dict[key] = {'text': text, 'count': 0}
    issues_data = models.Issues.objects.filter(project_id=project_id).values('status').annotate(
        ct=Count('id'))  # Count根据问题类型数量进行分组
    for item in issues_data:
        status_dict[item['status']]['count'] = item['ct']
    user_list = models.ProjectUser.objects.filter(project_id=project_id).values_list('user_id', 'user__username')
    top_ten = models.Issues.objects.filter(project_id=project_id, assign__isnull=False).order_by('-id')[0:10]
    context = {
        'status_dict': status_dict,
        'user_list': user_list,
        'top_ten': top_ten
    }
    return render(request, 'personal_management/dashboard.html', context)


def issues_chart(request, project_id):
    # "在简介页面显示问题折线图"
    #
    # today = datetime.datetime.now()  # 当前时间
    # # 处理数据
    # date_dict = collections.OrderedDict()  # 创建一个有序字典
    # # 循环当前时间后的面的30天
    # for i in range(0, 30):
    #     date = today - datetime.timedelta(days=i)  # 得到当前时间往后30天的时间
    #     # 得到一下这种格式
    #     # 2019-5-28:[116545446,0]   ----[时间戳，数量]
    #     date_dict[date.strftime("%Y-%m-%d")] = [time.mktime(date.timetuple()) * 1000, 0]
    # # 下面数据相当于 sql语句 select xxxx,1 as ctime from xxxx
    # # select id, name, email from table;
    # # select id, name, strftime("%Y-%m-%d", creator_datetime) as ctime from table;
    # # 就是创建了一个字段，利用strftime函数创建一个ctime字段，里面记录创建时间
    # # mysql数据库 "DATE_FORMATE(web_issues.crearor_datetime,'%%Y-%%m-%%d')"
    # result = models.Issues.objects.filter(project_id=project_id,
    #                                       creator_datetime__gte=today - datetime.timedelta(days=30)).extra(
    #     select={'ctime': "strftime('%%Y-%%m-%%d', web_issues.creator_datetime)"}).values('ctime').annotate(
    #     ct=Count('id'))
    # data_list = [
    #     # [15546464545, 9],
    #     # [15546464545, 9],
    #     # [15546464545, 9],
    #     # [15546464545, 9],
    # ]
    # for item in result:
    #     date_dict[item['ctime']][1] = item['ct']
    #
    # return JsonResponse({'status': True, 'data': list(date_dict.values())})
    """ 在概览页面生成highcharts所需的数据 """
    today = datetime.datetime.now().date()
    date_dict = collections.OrderedDict()
    for i in range(0, 30):
        date = today - datetime.timedelta(days=i)
        date_dict[date.strftime("%Y-%m-%d")] = [time.mktime(date.timetuple()) * 1000, 0]

    # select xxxx,1 as ctime from xxxx
    # select id,name,email from table;
    # select id,name, strftime("%Y-%m-%d",create_datetime) as ctime from table;
    # "DATE_FORMAT(web_transaction.create_datetime,'%%Y-%%m-%%d')"

    result = models.Issues.objects.filter(project_id=project_id,
                                          creator_datetime__gte=today - datetime.timedelta(days=29)).extra(
        select={'ctime': "strftime('%%Y-%%m-%%d',web_issues.creator_datetime)"}).values('ctime').annotate(ct=Count('id'))
    for item in result:
        date_dict[item['ctime']][1] = item['ct']
    return JsonResponse({'status': True, 'data': list(date_dict.values())})

